package pack0826;

import java.util.*;

public class Work {
    //1.ArrayList、Vector和Stack的区别？
    //ArrayList是线程不安全的，没上锁的，
    //Vector是线程安全的，上锁的
    //Stack是线程安全的，上锁的（栈）继承了Vector
    //2.分别对三个集合执行操作：添加元素、插入元素、获取元素、获取长度、清空集合等操作
    public static void a1(){
        List<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(23);
        list.add(34);
        list.add(22);
        list.add(33);
        list.add(44);
        System.out.println(list);

        list.add(2,55);
        System.out.println(list);

        list.remove(3);
        System.out.println(list);

        int i = list.get(4);
        System.out.println(i);

        int size = list.size();
        System.out.println(size);

        list.clear();
        System.out.println(list);
    }

    public static void a2(){
        Vector<Integer> vector = new Vector<>();
        vector.add(12);
        vector.add(23);
        vector.add(34);
        vector.add(22);
        vector.add(33);
        vector.add(44);
        System.out.println(vector);

        vector.add(2,55);
        System.out.println(vector);

        vector.remove(2);
        System.out.println(vector);

        int i = vector.get(4);
        System.out.println(i);

        int size = vector.size();
        System.out.println(size);

        vector.clear();
        System.out.println(vector);
    }

    public static void a3(){
        Stack<Integer> stack = new Stack<>();
        stack.add(12);
        stack.add(23);
        stack.add(34);
        stack.add(22);
        stack.add(33);
        stack.add(44);
        System.out.println(stack);

        stack.add(2,55);
        System.out.println(stack);

        stack.remove(2);
        System.out.println(stack);

        int i = stack.get(2);
        System.out.println(i);

        int size = stack.size();
        System.out.println(size);

        stack.clear();
        System.out.println(stack);
    }
    //3.向集合中随机添加10个元素，找到最大值和最小值
    public static void b(){
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i<10; i++){
            list.add((int)(Math.random()*50));
        }
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        System.out.println("最大值为"+list.get(9));
        System.out.println("最小值为："+list.get(0));
    }
    //4.用户输入一个内容，判断在集合中是否存在
    public static void c(){
        Scanner in = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i<10; i++){
            list.add((int)(Math.random()*20));
        }
        System.out.println("请输入一个0-20的整数：");
        int i = in.nextInt();
        System.out.println(list);
        boolean b = list.contains(i);
        System.out.println(b);
    }
    //5.产生一个随机集合，对这个集合进行排序
    public static void d(){
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i<10; i++){
            list.add((int)(Math.random()*50));
        }
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
    //6.产生一个对象集合，对这个集合进行排序(排序规则自定)
    public static void e(){
        List list = new ArrayList<>();
        Users u1 = new Users(3,"张三",125.3);
        Users u2 = new Users(5,"王五",89.8);
        Users u3 = new Users(1,"马六",125.3);
        Users u4 = new Users(10,"李四",145.2);
        Users u5 = new Users(8,"宋七",89.8);
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
    public static void main(String[] args) {
        b();
    }
}
